// 使用Promise封装wx.request  ， axios的封装原理 （XMLHttpRequest、Promise）
const {
    BASE_URL,
    ID,
    KEY
} = require('../config/index')
function $http({
    url,
    method = "GET",
    data = {}
}) {
    return new Promise((resolve, reject) => {
        wx.request({
            url: BASE_URL+ '/1.1' + url,
            method,
            data,
            header: {
                "X-LC-Id": ID,
                "X-LC-Key": KEY,
                "Content-Type": "application/json"
            },
            success: (res) => {
                resolve(res)
            },
            fail: (err) => {
                reject(err)
            }
        })
    })
}

function $get(url, data) {
    return $http({
        url,
        data
    })
}

function $post(url, data) {
    return $http({
        url,
        data,
        method: 'POST'
    })
}

module.exports = {
    $http,
    $get,
    $post
}